package mashibing.freshman.class03;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * 哈希表和有序表
 * 1. 哈希表
 * 1.1 内部按值传递
 * 1.2 内部按引用传递
 *
 */
public class Code05_HashMapTreeMap {

	public static class Node {
		public int value;

		public Node(int v) {
			value = v;
		}
	}

	// (K V)表
	public static void main(String[] args) {
		System.out.println("===Hash表HashMap================");
		HashMap<String, String> map = new HashMap<>();
		map.put("zuochengyun", "我是左程云");//新增
		System.out.println(map.containsKey("zuochengyun"));// true
		System.out.println(map.containsKey("zuo"));// false
		System.out.println(map.get("zuochengyun"));// 我是左程云

		map.put("zuochengyun", "他是左程云");// 更新（put新增或更新）
		System.out.println(map.get("zuochengyun"));

		// 哈希表 增删改查都是 O（1）的复杂度，但是比加减这种常数时间大的多
//		map.remove("zuochengyun");
//		System.out.println(map.containsKey("zuochengyun"));
//		System.out.println(map.get("zuochengyun"));

		String test1 = "zuochengyun";
		String test2 = "zuochengyun";
		System.out.println(map.containsKey(test1));
		System.out.println(map.containsKey(test2));

		HashMap<Integer, String> map2 = new HashMap<>();
		map2.put(1234567, "我是1234567");

		Integer a = 1234567;
		Integer b = 1234567;

		System.out.println(a == b);
		// HashMap原生类型（Integer Double Float String Character Long），按值传递
		System.out.println(map2.containsKey(a));
		System.out.println(map2.containsKey(b));

		// HashMap非原生类型按引用传递
		Node node1 = new Node(1);
		Node node2 = new Node(1);
		HashMap<Node, String> map3 = new HashMap<>();
		map3.put(node1, "我进来了！");
		System.out.println("node1:" + map3.containsKey(node1));
		System.out.println("node2:" + map3.containsKey(node2));

		System.out.println("===有序表TreeMap================");
		// TreeMap 增删改查 的时间复杂度：O（N）
		TreeMap<Integer, String> treeMap1 = new TreeMap<>();
		treeMap1.put(3, "我是3");
		treeMap1.put(0, "我是3");
		treeMap1.put(7, "我是3");
		treeMap1.put(2, "我是3");
		treeMap1.put(5, "我是3");
		treeMap1.put(9, "我是3");

		System.out.println(treeMap1.containsKey(7));// true
		System.out.println(treeMap1.containsKey(6));// false
		System.out.println(treeMap1.get(3));

		treeMap1.put(3, "他是3");// 更新
		System.out.println(treeMap1.get(3));

		treeMap1.remove(3);// 删除
		System.out.println(treeMap1.get(3));

		// TreeMap按key的大小排序
		System.out.println(treeMap1.firstKey());// 最小
		System.out.println(treeMap1.lastKey());// 最大
		// <=5 离5最近的key告诉我
		System.out.println(treeMap1.floorKey(5));
		// <=6 离6最近的key告诉我
		System.out.println(treeMap1.floorKey(6));
		// >=5 离5最近的key告诉我
		System.out.println(treeMap1.ceilingKey(5));
		// >=6 离6最近的key告诉我
		System.out.println(treeMap1.ceilingKey(6));

		// TreeMap 的key需要能比较，自定义的类型需要定义比较器
//		Node node3 = new Node(3);
//		Node node4 = new Node(4);
//		TreeMap<Node, String> treeMap2 = new TreeMap<>();
//		treeMap2.put(node3, "我是node3");
//		treeMap2.put(node4, "我是node4");

	}

}
